﻿using System;

namespace Carbon.Web.Forms.jQueryForms
{
    public class DateTimeEditor : TextBoxEditor
    {
		#region Properties (1)
		public override object Value
        {
            get
            {
                return GetDateTimeFromObject(base.Value);
            }
            set
            {
                if (Editor.ShowDate && Editor.ShowTime)
                {
                    base.Value = string.Format("{0:g}", value);
                }
                else if (Editor.ShowDate && !Editor.ShowTime)
                {
                    base.Value = string.Format("{0:d}", value);
                }
                else if (!Editor.ShowDate && Editor.ShowTime)
                {
                    base.Value = string.Format("{0:t}", value);
                }
                else
                {
                    throw new ArgumentOutOfRangeException("Control must have ShowDate='true', ShowTime='true' or some other combination of values."); 
                }
            }
        }
		#endregion

		#region Constructors (1)
		public DateTimeEditor(SimpleEditor editor)
            : base(editor)
        {

        }
		#endregion

		#region ASP Processing

		protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

             if ((Editor.ShowDate || Editor.ShowTime) == false)  throw new InvalidOperationException("(Editor.ShowDate || Editor.ShowTime) == false");
        }

		#endregion

		#region Methods (2)

		protected override string GetClientScript()
        {
            return string.Format("Carbon.Web.Forms.jQueryForms.DateTimeEditor(\"{0}\", {1}, {2}, {3});", 
                this.ClientID, 
                ConvertInvariant.ToString(this.Editor.CanBeNull).ToLower(),
                ConvertInvariant.ToString(this.Editor.ShowDate).ToLower(),
                ConvertInvariant.ToString(this.Editor.ShowTime).ToLower()
                );
        }

		private DateTime? GetDateTimeFromObject(object value)
        {
            DateTime? result = null;

            if (value == null)
            {
                result = null;
            }
            else
            {
                if (value is DateTime)
                {
                    result = (DateTime)value;
                }
                else
                {
                    String sValue = (String)value;

                    if (!String.IsNullOrEmpty(sValue))
                    {
                        result = Convert.ToDateTime(sValue);
                    }
                    else
                    {
                        if (Editor.IsTypeNullable)
                            return null;
                        else
                            throw new NullReferenceException(Errors.InvalidValue);
                    }
                }
            }

            return result;
        }

		#endregion
    }
}